package List;

public class MyLinkedList implements IList{
    public class ListNode{
        public int data;
        public ListNode next;
        public ListNode prev;

        public ListNode(int data) {
            this.data = data;
        }
    }
    public ListNode head;
    public ListNode last;

    @Override
    public void addFirst(int data) {
        ListNode newnode = new ListNode(data);
        if(head == null){
            head = last = newnode;
        }else {
            newnode.next = head;
            head.prev = newnode;
            head = newnode;
        }

    }

    @Override
    public void addLast(int data) {
        ListNode newnode = new ListNode(data);
        if(head == null){
            head = last = newnode;
        }else {
            last.next = newnode;
            newnode.prev = last;
            last = newnode;
        }
    }

    private void CheckIndex(int index){
        if(index < 0 || index > size()){
            throw new IllegalIndexException("index位置不合法");
        }
    }

    private ListNode FindIndexnode(int index){
        ListNode cur = head;
        while(index-1 > 0){
            cur = cur.next;
            index--;
        }


        return cur;
    }

    @Override
    public void addIndex(int index, int data) {
        try {
            CheckIndex(index);
            if(index == 0){
                addFirst(data);
            }
            if(index == size()){
                addLast(data);
            }
            ListNode newnode = new ListNode(data);
            ListNode cur = FindIndexnode(index);
            newnode.next = cur;
            cur.prev.next = newnode;
            newnode.prev = cur.prev;
            newnode.next = cur;
        }catch(IllegalIndexException e){
            e.printStackTrace();
        }
    }



    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.data == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.data == key){
            if(cur == head){
                head = head.next;
                if(head != null){
                    head.prev = null;
                }
            }else {
                cur.prev.next = cur.next;
                if (cur.next == null) {
                    last = last.prev;
                } else {
                    cur.next.prev = cur.prev;
                }
            }
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.data == key){
                if(cur == head){
                    head = head.next;
                    if(head != null){
                        head.prev = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur.next == null) {
                        last = last.prev;
                    } else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode Curn = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = Curn;
        }
        head = last = head;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
